package com.summer.dynamicDataType;

import java.lang.reflect.ParameterizedType;
import java.util.HashMap;

public class DynamicData {
	private Object valueObject;
	private static HashMap<Class,Object> map = new HashMap<>();
	static{
		map.put(Integer.class,1);
		map.put(String.class,1);
	}
	public DynamicData(){}
	
	public DynamicData(Object valueObject){
		this.valueObject = valueObject;
	}
	
	@SuppressWarnings("unchecked")
	public <T>T value(){
		try{
			Class <T> entityClass = (Class <T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; 

			if(map.get(entityClass) instanceof Integer ){
				Object obj = Integer.parseInt(valueObject.toString());
				return (T) obj;
			}
			if(map.get(entityClass) instanceof String ){
				return (T) valueObject.toString();
			}
				
			return (T)valueObject;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
}
